package liming.tool.handle.net.root.base;

import liming.tool.handle.net.root.error.NetToolsException;

import java.net.InetAddress;

/**
 * 包装一个套接字信息
 */
public class IP {
    protected InetAddress inetAddress;
    protected int port;

    public IP() {
    }

    public IP(InetAddress inetAddress, int port) {
        setIP(inetAddress, port);
    }

    public IP(IP ip) {
        setIP(ip.inetAddress, ip.port);
    }

    public IP setIP(InetAddress inetAddress, int port) {
        this.inetAddress = inetAddress;
        this.port = port;
        return this;
    }

    public InetAddress getInetAddress() throws NetToolsException {
        if (inetAddress == null) throw new NetToolsException("no set inetAddress");
        return inetAddress;
    }

    public int getPort() throws NetToolsException {
        if (port == 0) throw new NetToolsException("no set port");
        return port;
    }

    public void clear() {
        inetAddress = null;
        port = -1;
    }

    @Override
    public String toString() {
        return "IP{" +
                "inetAddress=" + inetAddress +
                ", port=" + port +
                '}';
    }
}
